package com.aphrodite.ui.widgets;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class HorizontalList extends TraversableWidget {
	Vector list = new Vector();
	int currentIndex;
	int startIndex;
	int endIndex;
	private byte location;
	public final static byte TOP = 5;
	public final static byte BOTTOM = 10;
	final static byte LEFT = 15;
	final static byte RIGHT = 20;

	public HorizontalList(Theme theme, int width, byte textLocation) {
		super(theme, width, 0);
		this.location = textLocation;
	}

	public HorizontalList(Theme theme, int width) {
		this(theme, width, BOTTOM);

	}

	protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
		return false;
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, true);
		ListItem item;
		int currX = 2;
		for (int i = startIndex; i < endIndex; i++) {
			item = (ListItem) list.elementAt(i);
			renderItem(g, item, currX, i == currentIndex);
			/* item width + pad between items */
			currX += item.itemWidth + 5;
		}
		release(g, width, height, true);
	}

	private void increaseIndex() {
		if (currentIndex < endIndex - 1) {
			currentIndex++;
		}
		reDrawWidget();
	}

	private void decreaseIndex() {
		if (currentIndex > 0) {
			currentIndex--;
		}
		reDrawWidget();
	}

	private void scrollLeft() {

	}

	private void scrollRight() {

	}

	public boolean moveLeft() {
		decreaseIndex();
		return true;
	}

	public boolean moveRight() {
		increaseIndex();
		return true;
	}

	protected void renderItem(Graphics g, ListItem item, int x, boolean isSelected) {
		if (isSelected)
			renderSelector(g, item, x);
		int iX = 0;
		int iY = 0;
		int sX = 0;
		int sY = 0;
		switch (location) {
		case LEFT:
			break;
		case RIGHT:
			break;
		case TOP:
			break;
		default:/* Bottom */
			iX = (x + item.itemWidth / 2) - (item.icon != null ? item.icon.getWidth() / 2 : 0);
			iY = 2;
			if (theme.bmFont == null) {
				sX = (x + item.itemWidth / 2) - theme.widgetFont.stringWidth(item.text) / 2;
			} else {
				sX = (x + item.itemWidth / 2) - theme.bmFont.stringWidth(item.text) / 2;
			}
			sY = item.icon == null ? item.itemHeight / 2 : item.icon.getHeight() + 2;
		}

		if (item.icon != null) {
			g.drawImage(item.icon, iX, iY, 0);
		}

		if (item.text != null) {
			g.setColor(isSelected ? theme.highLightFGColor : theme.fgColor);
			if (theme.bmFont == null) {
				g.setFont(theme.widgetFont);
				g.drawString(item.text, sX, sY, 0);
			} else {
				theme.bmFont.drawString(g, item.text, sX, sY, 0);
			}
		}
	}

	protected void renderSelector(Graphics g, ListItem item, int x) {
		g.setColor(theme.highLightColor);
		g.fillRoundRect(x, 2, item.itemWidth, item.itemHeight, 3, 3);
	}

	/**
	 * 
	 * @param text
	 * @param icon
	 * @return the width and height of the item in a integer array of length 2
	 */
	protected int[] computeDimensions(String text, Image icon) {
		int sw = 0;
		int iw = 0;
		int sh = 0;
		int ih = 0;
		if (text != null) {
			if (theme.bmFont == null) {
				sw = theme.widgetFont.stringWidth(text);
				sh = theme.widgetFont.getHeight();
			} else {
				sw = theme.bmFont.stringWidth(text);
				sh = theme.bmFont.getHeight();
			}
		}
		if (icon != null) {
			iw = icon.getWidth();
			ih = icon.getHeight();
		}
		switch (location) {
		case LEFT:
		case RIGHT:
			return new int[] { iw + 2 + sw, Math.max(ih, sh) };
		case TOP:
		default:/* Bottom */
			return new int[] { Math.max(iw, sw), sh + 2 + ih };
		}

	}

	public void addItem(String item, Image icon) {
		ListItem lItem = new ListItem(item, icon, computeDimensions(item, icon));
		list.addElement(lItem);
		/* Adjust the height */
		if (lItem.itemHeight > height)
			height = lItem.itemHeight + 4;
		updateModel();
		reDrawWidget();
		stateChanged(this);
	}

	protected void updateModel() {
		currentIndex = 0;
		startIndex = 0;
		endIndex = list.size();
	}

	public void resizeToFit() {

	}

	final class ListItem {
		final String text;
		final Image icon;
		int itemWidth;
		int itemHeight;

		ListItem(String text, Image icon, int[] dimensions) {
			this.text = text;
			this.icon = icon;
			this.itemWidth = dimensions[0];
			this.itemHeight = dimensions[1];
		}
	}
}
